package com.itcast.reflect;

import com.itcast.domain.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo4 {
    public static void main(String[] args) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        // 1.获取 class 对象
        Class<Person> personClass = Person.class;

        // 2.获取 public 成员方法
        Method[] methods = personClass.getMethods();
        for (Method method : methods) {
            System.out.println(method);
            // 获取方法名称
            String name = method.getName();
            System.out.println(name);
        }
        System.out.println("--------");

        // 获取指定方法
        Method method = personClass.getMethod("eat");
        Person person = new Person();
        // 执行方法
        method.invoke(person);

        Method method1 = personClass.getMethod("eat", String.class);
        method1.invoke(person, "大盘鸡");
        System.out.println("---------");

        // 获取类名的方法
        String className = personClass.getName();
        System.out.println(className);

    }
}
